using System;
using Sedge.Core.Configuration;
using Sedge.Core.Execution;

namespace Sedge.UI.Controller
{
	public abstract class SimpleControllerBase<T> : IStepController where T : class, ITransferable, new()
	{
		public Suite Suite { get; set;}
		public Options Options { get; set; }

		private T _view;
		public object View
		{
			get
			{
				if (_view == null)
				{
					_view = CreateView();
				}

				return _view;
			}
		}

		public T StepView
		{
			get { return (T)View; }
		}

		public string Caption
		{
			get
			{
				string caption = Options.Caption();
				if (String.IsNullOrEmpty(caption))
					caption = GetControllerCaption();

				return caption;
			}
		}

		public void Initialize(Suite suite, Options options)
		{
			Suite = suite;
			Options = options;
			
			OnInitialize();
		}

		public bool EnterStep(bool fromPrev)
		{
			return OnEnterStep(fromPrev);
		}

		public bool LeaveStep(bool toNext)
		{
			if (toNext)
			{
				OnTransferData();
			}

			return true;
		}

		abstract protected T CreateView();
		abstract protected string GetControllerCaption();

		virtual protected void OnInitialize() {}
		
		virtual protected void OnTransferData()
		{
			StepView.TransferData();
		}

		virtual protected bool OnEnterStep(bool fromPrev) 
		{
			return true; 
		}

	}
}